<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\ClassifyModel;

class Classify extends Base
{
    // 分类列表
    public function index()
    {
//            halt(request()->isAjax());
        if(request()->isAjax()){
            $param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            $where = ['pid'=>'0'];
            if (!empty($param['searchText'])) {
                $where['style'] = ['like', '%' . $param['searchText'] . '%'];
            }

            $classify = new ClassifyModel();
            $selectResult = $classify->getClassifyByWhere($where, $offset, $limit);

            foreach($selectResult as $key=>$vo){
//                $selectResult[$key]['thumbnail'] = '<img src="' . $vo['thumbnail'] . '" width="40px" height="40px">';
                $selectResult[$key]['operate'] = showOperate($this->makeButton($vo['id']));
            }

            $return['total'] = $classify->getAllClassify($where);  // 总数据
            $return['rows'] = $selectResult;
            return json($return);
        }

        return $this->fetch();
    }

    // 添加一级分类
    public function classifyAdd()
    {
        if(request()->isPost()){
            $param = input('post.');

            unset($param['file']);

            $classify = new ClassifyModel();
            $flag = $classify->addClassify($param);

            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }

        return $this->fetch();
    }
    //添加子分类活动
    public function classifySdy($id)
    {
        if(request()->isPost()){
            $param = input('post.');

            unset($param['file']);

            $classify = new ClassifyModel();
            $flag = $classify->addClassifyS($param);

            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }

        return $this->fetch('admin@classify/classifysdy',['id'=>$id]);
    }
    //分类删除
    public function classifyDel()
    {
        $id = input('param.id');
        $classifyfd = new ClassifyModel();
//        dump($classifyfd->getOneclassifyFd($id));
        if ($classifyfd->getOneclassify($id)){
            return msg(1, '', '请先删除您的子分类');
        }else{
            $classify = new ClassifyModel();
            $flag = $classify->delClassify($id);
            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
    }

    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id)
    {
        return [
            '添加子分类' => [
                'auth' => 'classify/classifyadd',
                'href' => url('classify/classifysdy', ['id' => $id]),
                'btnStyle' => 'primary',
                'icon' => 'fa fa-paste'
            ],
            '查看子分类'=>[
                'auth' => 'classify/classifyadd',
                'href' => url('classifycld/index', ['id' => $id]),
                'btnStyle' => 'info',
                'icon' => 'fa fa-institution'
            ],
            '删除' => [
                'auth' => 'articles/articledel',
                'href' => "javascript:classifyDel(" . $id . ")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ]
        ];
    }
}
